#ifndef _TCP_SESSIONEX_H
#define _TCP_SESSIONEX_H
#include <uv.h>
#include <mutex>
#include <string>
#include "Session.h"
#include "Timer.h"
class CTCPClient;
typedef std::shared_ptr<CTCPClient> CTCPClientPtr;
typedef std::function<void(CTCPClientPtr, int)> ConnCb;
class CTCPClient :public std::enable_shared_from_this <CTCPClient>
{
public:
	template<class C = CTCPClient> static CTCPClientPtr NewTCPClien(uv_loop_t* loop){
		CTCPClientPtr client(new CTCPClient(loop));		
		return client;
	}	
	virtual ~CTCPClient();
	bool Connect(char* ip, size_t port);
	void Write(char* buf, size_t bsize);
	void OnMsgCall(SessionReadCb msgcb);
	void OnConnStatus(ConnCb msgcb){ _connStatus = msgcb; }
	void OnClose(SessionCloseCb closecb);
	void SetReConnInteral(size_t ms){ _interanl_ms = ms ;}	
	void CloseConn();
	static void _OnConnectCb(uv_connect_t* req, int status);	
	static void  _ReConnect(void *usrData){
		CTCPClient* clitn = (CTCPClient*)usrData;		
		clitn->Connect((char*)clitn->_perrip.c_str(), clitn->_perrport);
	}
private:
	CTCPClient(uv_loop_t* loop);
private:
	uv_tcp_t*  _socket ;			
	uv_connect_t*  _connect;		
	uv_loop_t* _loop = nullptr;			
public:	
	bool _bconn = false;	
	SessionPtr _session = nullptr;	
	size_t _interanl_ms = 0; //0 	
	ConnCb _connStatus = nullptr;
	SessionReadCb _msgcb = nullptr;
	SessionCloseCb _closecb = nullptr;
	std::string _perrip;
	size_t	_perrport;
	CTimer m_timer;
};
#endif

